<?php
if(!isset($dbselect)) //前面没设置 数据库 则指定使用 DB3
    $dbselect   = Db::G3();
///关于标收费信息的统计
$sfwhere      = "";
if($startdate!="" && $enddate!=""){
    $sfwheref    = " and checktime >='$startdate' and  checktime<='$enddate'"; 
    $sfwheres    = " and time_h >='$startdate' and  time_h<='$enddate'"; 
    $sfwherev    = " and verify_time >='$startdate' and  verify_time<='$enddate'";
    $sfwheresub  = " and subscribe_time >='$startdate' and  subscribe_time<='$enddate'";
   
    $sfwhereb    = " and btime >='$startdate' and  btime<='$enddate'";
    $daywhere    = " and day >='$startdate' and  day<='$enddate'";
} 
$gfileds        = "SUBSTR(time_h,1,10 )";
$v_gfileds        = "SUBSTR(verify_time,1,10 )";
if($MOD=="M"){
    $gfileds    = "SUBSTR(time_h,1,7)";    
}
$gfiledb        = "SUBSTR(btime,1,10 )";
if($MOD=="M"){
    $gfiledb    = "SUBSTR(btime,1,7)";    
}
$gfiledsub        = "SUBSTR(subscribe_time,1,10 )";
if($MOD=="M"){
    $gfiledsub    = "SUBSTR(subscribe_time,1,7)";    
}
 
$rows = $dbselect->GetRows("SELECT $gfileds '认证时间',count(personid) AS '认证数'
FROM 01u_9personid
WHERE 1 $sfwheres
GROUP BY $gfileds ");
foreach($rows as &$row)
{   
    $ctime  = $row['认证时间'];
    $count  = $row['认证数'];
    CTLog::G()->pxxDayReal("sid",1,"实名_认证_新增",$count,$ctime);
}

$rows = $dbselect->GetRows("SELECT $gfileds '认证时间',count(iskoufei) AS '扣费'
FROM  01u_9personid_f
WHERE iskoufei=1 $sfwheres
GROUP BY $gfileds ");
foreach($rows as &$row)
{   
    $ctime  = $row['认证时间'];
    $count  = $row['扣费'];
    CTLog::G()->pxxDayReal("sid",1,"实名_认证_扣费",$count,$ctime);
}

//新增VIP  投资人
$rows = $dbselect->GetRows("SELECT 
sum(if(fuid_isTender(uid),-money,0)) as 会员费, 
sum(if(fuid_isTender(uid),1,0))  as 会员次数,
$gfileds as qtime_h,fuid_GetCid(uid) as cid 
FROM  `04a_5flow_uid` f 
WHERE flowty='vip' AND INSTR(note,'续费扣款')=0 $sfwheres
GROUP by $gfileds");
foreach($rows as &$row){
    $ctime  = $row['qtime_h'];
    $count  = $row['会员次数'];
    $fei    = $row['会员费'];
    $cid    = $row['cid'];
    
    if($count>0){
        CTLog::G()->pxxDayReal($count.",sid",1,"新增VIP",$fei,$ctime);
    }
}

//分站新增VIP  投资人
$rows = $dbselect->GetRows("SELECT
		sum(if(fuid_isTender(uid),-money,0)) as 会员费,
		sum(if(fuid_isTender(uid),1,0))  as 会员次数,
		$gfileds as qtime_h,fuid_GetCid(uid) as cid
		FROM  `04a_5flow_uid` f
		WHERE flowty='vip' AND INSTR(note,'续费扣款')=0 $sfwheres
		GROUP by cid ");
foreach($rows as &$row){
	$ctime  = $row['qtime_h'];
	$count  = $row['会员次数'];
	$fei    = $row['会员费'];
	$cid    = $row['cid'];

	if($count>0){
		CTLog::G()->pxxDayReal($count.",cid",$cid,"新增VIP",$fei,$ctime);
	}
}

$lower_date = $startdate ? $startdate : date("Y-m-d 00:00:00"); //小于今天的
//新增有效投资人数
$rows = $dbselect->GetRows("SELECT count(distinct uid) as 新增有效投资人,sum(money) as 新增有效充值,$gfileds as qtime_h 
		from 04a_3applyqueue WHERE aptype in (4,7) and uid_ty=1 and status=3 $sfwheres 
		AND uid not in (select distinct uid from 04a_3applyqueue WHERE aptype in (4,7) and uid_ty=1 and status=3 and time_h<'$lower_date')
		 ");
//print_r($rows);
foreach($rows as &$row){
	$ctime  = $row['qtime_h'];
	$count  = $row['新增有效投资人'];
	$fei    = $row['新增有效充值'];
	
	if($count>0){
		CTLog::G()->pxxDayReal($count.",sid",1,"新增有效投资人",$fei,$ctime);
	}
}

//新增有效投资人数,分站
$rows = $dbselect->GetRows("SELECT count(distinct uid) as 新增有效投资人,sum(money) as 新增有效充值,$gfileds as qtime_h,cid
		from 04a_3applyqueue WHERE aptype in (4,7) and uid_ty=1 and status=3 $sfwheres
		AND uid not in (select distinct uid from 04a_3applyqueue WHERE aptype in (4,7) and uid_ty=1 and status=3 and time_h<'$lower_date')
		 GROUP by cid  ");
//print_r($rows);
foreach($rows as &$row){
	$ctime  = $row['qtime_h'];
	$count  = $row['新增有效投资人'];
	$fei    = $row['新增有效充值'];
	$cid    = $row['cid'];

	if($count>0){
		CTLog::G()->pxxDayReal($count.",cid",$cid,"新增有效投资人",$fei,$ctime);
	}
}


//有效投资人累计总数----总站  add by clw 2016-03-18
$rows = $dbselect->GetRows("SELECT count(distinct uid) as 有效投资人数,sum(money) as 有效投资人充值,$gfileds as qtime_h 
		from 04a_3applyqueue WHERE aptype in (4,7) and uid_ty=1 and status=3 $sfwheres ");
//print_r($rows);
foreach($rows as &$row){
	$ctime  = $row['qtime_h'];
	$count  = $row['有效投资人数'];
	$fei    = $row['有效投资人充值'];
       
	
	if($count>0){
		CTLog::G()->pxxDayReal($count.",sid",1,"有效投资人投资资金",$fei,$ctime);
                CTLog::G()->pxxDayReal($count.",sid",1,"有效投资人总数",$count,$ctime);
	}
}


//有效投资人累计------分站  add by clw 2016-03-18
$rows = $dbselect->GetRows("SELECT count(distinct uid) as 有效投资人数,sum(money) as 有效投资人充值,$gfileds as qtime_h,cid
		from 04a_3applyqueue WHERE aptype in (4,7) and uid_ty=1 and status=3 $sfwheres
		 GROUP by cid  ");
//print_r($rows);
foreach($rows as &$row){
	$ctime  = $row['qtime_h'];
	$count  = $row['有效投资人数'];
	$fei    = $row['有效投资人充值'];
        $cid    = $row['cid'];
	if($count>0){
		CTLog::G()->pxxDayReal($count.",cid",$cid,"有效投资人投资资金",$fei,$ctime);
                CTLog::G()->pxxDayReal($count.",cid",$cid,"有效投资人总数",$count,$ctime);
	}
}


$rows = $dbselect->GetRows("SELECT $gfiledsub '关注时间',count(subscribe) AS '关注数'
FROM 09wx_u_info
WHERE subscribe=1 $sfwheresub
GROUP BY $gfiledsub ");
foreach($rows as &$row)
{   
    $ctime  = $row['关注时间'];
    $count  = $row['关注数'];
    CTLog::G()->pxxDayReal("sid",1,"微信_关注_总",$count,$ctime);
}


$rows = $dbselect->GetRows("SELECT $gfiledb '绑定时间',count(subscribe) AS '绑定数'
FROM 09wx_u_info
WHERE subscribe=1 and uid>0 $sfwhereb
GROUP BY $gfiledb ");
foreach($rows as &$row)
{   
    $ctime  = $row['绑定时间'];
    $count  = $row['绑定数'];
    CTLog::G()->pxxDayReal("sid",1,"微信_绑定_总",$count,$ctime);
}

$rows = $dbselect->GetRows("SELECT $gfiledb '绑定时间',count(subscribe) AS '绑定数'
FROM 09wx_u_info
WHERE subscribe=1 and uid>0 $sfwhereb
GROUP BY $gfiledb ");
foreach($rows as &$row)
{   
    $ctime  = $row['绑定时间'];
    $count  = $row['绑定数'];
    CTLog::G()->pxxDayReal("sid",1,"微信_绑定_总",$count,$ctime);
}

//注册渠道
$rows = $dbselect->GetRows("SELECT $gfileds '注册时间',count(uid) AS '用户数',way,fuid_GetCid(uid) as cid 
FROM 01u_0info
WHERE fuid_isTender(uid)=1 $sfwheres
GROUP BY way,$gfileds ");
foreach($rows as &$row)
{   
    $ctime  = $row['注册时间'];
    $count  = $row['用户数'];
    $cid = $row["cid"];
    
    if($row['way']==1) {
        CTLog::G()->pxxDayReal("sid",1,"网站_注册_web",$count,$ctime);
    }else if($row['way']==2) {
        CTLog::G()->pxxDayReal("sid",1,"网站_注册_weixin",$count,$ctime);  
    }else if($row['way']==3) {
        CTLog::G()->pxxDayReal("sid",1,"网站_注册_app",$count,$ctime);
    }else if($row['way']) {
        CTLog::G()->pxxDayReal("sid",1,"网站_注册_其他",$count,$ctime);
    }
}

//分站注册渠道
$rows = $dbselect->GetRows("SELECT $gfileds '注册时间',count(uid) AS '用户数',way,fuid_GetCid(uid) as cid
		FROM 01u_0info
		WHERE fuid_isTender(uid)=1 $sfwheres
		GROUP BY way,cid ");
foreach($rows as &$row)
{
	$ctime  = $row['注册时间'];
	$count  = $row['用户数'];
	$cid = $row["cid"];

	if($row['way']==1) {
		CTLog::G()->pxxDayReal("cid",$cid,"网站_注册_web",$count,$ctime);
	}else if($row['way']==2) {
		CTLog::G()->pxxDayReal("cid",$cid,"网站_注册_weixin",$count,$ctime);
	}else if($row['way']==3) {
		CTLog::G()->pxxDayReal("cid",$cid,"网站_注册_app",$count,$ctime);
	}else if($row['way']) {
		CTLog::G()->pxxDayReal("cid",$cid,"网站_注册_其他",$count,$ctime);
	}
}

//注册渠道,总站
$sqlz = "SELECT $gfileds '注册时间',count(uid) AS '用户数',way,fuid_GetCid(uid) as cid
FROM 01u_0info
WHERE fuid_isBorrower(uid)=1 $sfwheres
GROUP by $gfileds ";

$rows = $dbselect->GetRows($sqlz);
//print_r($rows);
foreach($rows as &$row)
{   
    $ctime  = $row['注册时间'];
    $count  = $row['用户数'];
    CTLog::G()->pxxDayReal("sid",1,"网站_新增借款人",$count,$ctime);
}

//注册渠道
$sqlz_cid = "SELECT $gfileds '注册时间',count(uid) AS '用户数',way,fuid_GetCid(uid) as cid
FROM 01u_0info
WHERE fuid_isBorrower(uid)=1 $sfwheres
GROUP by cid ";

$rows = $dbselect->GetRows($sqlz_cid);
foreach($rows as &$row)
{
	$ctime  = $row['注册时间'];
	$count  = $row['用户数'];
	$cid = $row["cid"];
	CTLog::G()->pxxDayReal("cid",$cid,"网站_新增借款人",$count,$ctime);
}

//总站投资人资产
$sql="select day, sum(if(zc>0,1,0)) as c_总资产大于0,sum(if(zc>0,zc,0)) as v_总资产大于0,
sum(if(zc>10*10000,1,0)) as c_总资产大于10万,sum(if(zc>10*10000,zc,0)) as v_总资产大于10万,
sum(if((yue_keti+yue_buketi)>0,1,0)) as c_闲置资金,sum(yue_keti+yue_buketi) as v_闲置资金, 
sum(if((yue_keti+yue_buketi)>0,1,0)) as c_闲置资金,sum(yue_keti+yue_buketi) as v_闲置资金,
sum(if(keti_freeze>0,1,0)) as c_提现冻结,sum(keti_freeze) as v_提现冻结,
sum(if((daishou_bj+daishou_lx)>0,1,0)) as c_代收本息,sum(daishou_bj+daishou_lx) as v_代收本息,
fuid_GetCid(uid) as cid
from 01u_1daylog_zj  where fuid_isTender(uid)=1 $daywhere group by day";
$rows = $dbselect->GetRows($sql);
foreach($rows as &$row)
{ 
    $ctime  = $row['day'];
    $cid = $row['cid'];
    
    CTLog::G()->pxxDayReal($row['c_总资产大于0'].",sid",1,"投资人_资产0",$row['v_总资产大于0'],$ctime);
    CTLog::G()->pxxDayReal($row['c_总资产大于10万'].",sid",1,"投资人_资产10W",$row['v_总资产大于10万'],$ctime);
    CTLog::G()->pxxDayReal($row['c_闲置资金'].",sid",1,"投资人_资产闲置",$row['v_闲置资金'],$ctime);
    CTLog::G()->pxxDayReal($row['c_提现冻结'].",sid",1,"投资人_提现冻结",$row['v_提现冻结'],$ctime);
    CTLog::G()->pxxDayReal($row['c_代收本息'].",sid",1,"投资人_代收",$row['v_代收本息'],$ctime);
    
   
}

//分站投资人资产
$sql="select day, sum(if(zc>0,1,0)) as c_总资产大于0,sum(if(zc>0,zc,0)) as v_总资产大于0,
sum(if(zc>10*10000,1,0)) as c_总资产大于10万,sum(if(zc>10*10000,zc,0)) as v_总资产大于10万,
sum(if((yue_keti+yue_buketi)>0,1,0)) as c_闲置资金,sum(yue_keti+yue_buketi) as v_闲置资金,
sum(if((yue_keti+yue_buketi)>0,1,0)) as c_闲置资金,sum(yue_keti+yue_buketi) as v_闲置资金,
sum(if(keti_freeze>0,1,0)) as c_提现冻结,sum(keti_freeze) as v_提现冻结,
sum(if((daishou_bj+daishou_lx)>0,1,0)) as c_代收本息,sum(daishou_bj+daishou_lx) as v_代收本息,
fuid_GetCid(uid) as cid
from 01u_1daylog_zj  where fuid_isTender(uid)=1 $daywhere group by cid,day";
$rows = $dbselect->GetRows($sql);
foreach($rows as &$row)
{
	$ctime  = $row['day'];
	$cid = $row['cid'];

	CTLog::G()->pxxDayReal($row['c_总资产大于0'].",cid",$cid,"投资人_资产0",$row['v_总资产大于0'],$ctime);
	CTLog::G()->pxxDayReal($row['c_总资产大于10万'].",cid",$cid,"投资人_资产10W",$row['v_总资产大于10万'],$ctime);
	CTLog::G()->pxxDayReal($row['c_闲置资金'].",cid",$cid,"投资人_资产闲置",$row['v_闲置资金'],$ctime);
	CTLog::G()->pxxDayReal($row['c_提现冻结'].",cid",$cid,"投资人_提现冻结",$row['v_提现冻结'],$ctime);
	CTLog::G()->pxxDayReal($row['c_代收本息'].",cid",$cid,"投资人_代收",$row['v_代收本息'],$ctime);
}

//总站借款人资产
$sql="select day,sum(if(zc>0,1,0)) as c_总资产大于0,sum(if(zc>0,zc,0)) as v_总资产大于0,
sum(if(zc>10*10000,1,0)) as c_总资产大于10万,sum(if(zc>10*10000,zc,0)) as v_总资产大于10万,
fuid_GetCid(uid) as cid
from 01u_1daylog_zj  where fuid_isBorrower(uid)=1 $daywhere group by day";
$rows = $dbselect->GetRows($sql);
foreach($rows as &$row)
{ 
    $ctime  = $row['day'];
    $cid = $row['cid'];
    CTLog::G()->pxxDayReal($row['c_总资产大于0'].",sid",1,"借款人_资产0",$row['v_总资产大于0'],$ctime);
    CTLog::G()->pxxDayReal($row['c_总资产大于10万'].",sid",1,"借款人_资产10W",$row['v_总资产大于10万'],$ctime);
    
   
}

//分站借款人资产
$sql="select day,sum(if(zc>0,1,0)) as c_总资产大于0,sum(if(zc>0,zc,0)) as v_总资产大于0,
sum(if(zc>10*10000,1,0)) as c_总资产大于10万,sum(if(zc>10*10000,zc,0)) as v_总资产大于10万,
fuid_GetCid(uid) as cid
from 01u_1daylog_zj  where fuid_isBorrower(uid)=1 $daywhere group by cid";
$rows = $dbselect->GetRows($sql);
foreach($rows as &$row)
{
	$ctime  = $row['day'];
	$cid = $row['cid'];
	CTLog::G()->pxxDayReal($row['c_总资产大于0'].",cid",$cid,"借款人_资产0",$row['v_总资产大于0'],$ctime);
	CTLog::G()->pxxDayReal($row['c_总资产大于10万'].",cid",$cid,"借款人_资产10W",$row['v_总资产大于10万'],$ctime);
}


//标扩展金额统计 - 总站
$rows = Db::G()->GetRows("SELECT sum(account) as cls_money,count(bid) as cls_nums,classify,{$v_gfileds} as 发标日期   FROM 05b_0base WHERE status in (3,6) $sfwherev GROUP BY classify,{$v_gfileds} ");
$total_money = 0;
$total_nums = 0;
foreach($rows as &$row){
	$cls_money = $row["cls_money"];
	$cls_nums = $row["cls_nums"];
	$classify = $row["classify"];
	$ctime  = $row['发标日期'];
	$total_money += $cls_money;
	$total_nums += $cls_nums;
	
	CTLog::G()->pxxDayReal($total_nums.",sid",1,"标扩展_金额_".$classify,$cls_money,$ctime);
}

if(!cfg::jugebit4(64)){
	CTLog::G()->pxxDayReal($total_nums.",sid",1,"标扩展_金额_总额",$total_money,$ctime);
}



//标扩展比例统计 - 总站
foreach($rows as &$row){
	$cls_money = $row["cls_money"];
	$cls_nums = $row["cls_nums"];
	$classify = $row["classify"];
	$ctime  = $row['发标日期'];
	$cls_apr = 0;
	if ($total_money > 0) {
		$cls_apr = $cls_money/$total_money * 100;
	}
	
	CTLog::G()->pxxDayReal("0,sid",1,"标扩展_比例_".$classify,$cls_apr,$ctime);
}


//标扩展金额统计 - 分站
$rows = Db::G()->GetRows("SELECT sum(account) as cls_money,count(bid) as cls_nums,classify,{$v_gfileds} as 发标日期,cid FROM 05b_0base WHERE status in (3,6) $sfwherev GROUP BY classify,cid,{$v_gfileds} ");
$total_money = array();
$total_nums = array();
foreach($rows as &$row){
	$cls_money = $row["cls_money"];
	$cls_nums = $row["cls_nums"];
	$classify = $row["classify"];
	$ctime  = $row['发标日期'];
	$cid  = $row['cid'];
	$total_money[$cid] += $cls_money;
	$total_nums[$cid] += $cls_nums;
	CTLog::G()->pxxDayReal($cls_nums.",cid",$cid,"标扩展_金额_".$classify,$cls_money,$ctime);
}
foreach ($total_money as $cid=>$v_tot_money) {
	$c_nums = $total_nums[$cid];
	CTLog::G()->pxxDayReal($c_nums.",cid",$cid,"标扩展_金额_总额",$v_tot_money,$ctime);
} 

//标扩展比例统计 - 分站
foreach($rows as &$row){
	$cls_money = $row["cls_money"];
	$classify = $row["classify"];
	$ctime  = $row['发标日期'];
	$cls_apr = 0;
	if ($total_money[$cid] > 0) {
		$cls_apr = $cls_money/$total_money[$cid] * 100;
	}

	CTLog::G()->pxxDayReal("0,cid",1,"标扩展_比例_".$classify,$cls_apr,$ctime);
}




		
			
	



